﻿using DataServer.Entities;
using IsPay.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay.View
{
    public partial class BindingDebitCard : System.Web.UI.Page
    {
        public BankCardAccunt bcd = new BankCardAccunt();
        UserProfile loginuser;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null || Session["USERID"].ToString().Length <= 0|| Session["LOGINUSER"]==null)
            {
                Response.Write("<script>top.location='../Default.aspx'</script>");
            }
            if (Request.Params["ActionType"] != null)
            {
                loginuser = (UserProfile)Session["LOGINUSER"];
                if(loginuser.AccuntId==null|| loginuser.AccuntId.Length <= 0)
                {
                    lblmsg.Text = "当前登录用户未绑定账户，请先绑定账户";
                }
                
                String actionType = Request.Params["ActionType"].ToString();
                if (actionType.Equals("addsingle"))
                {
                    String returnmsge = addBankAccuntSingle();
                    lblmsg.Text = returnmsge;
                }
            }

         }
        private String addBankAccuntSingle()
        {
            String strResult = "OK";

            try
            {

                bcd = new BankCardAccunt();
                bcd.BankCardUserName = Request.Params["txtBankAccuntName"].ToString().Trim();
                bcd.BankCardBankName = Request.Params["txtBankAccuntBank"].ToString().Trim();
                bcd.BankCardSubName= Request.Params["txtBankAccuntSubBank"].ToString().Trim();
                bcd.BankCardNo = Request.Params["txtBankAccuntCardID"].ToString().Trim();
                bcd.BankCardPayPWD= Request.Params["txtrpBankAccuntPWD"].ToString().Trim();
                String bankcardnorp= Request.Params["txtrpBankAccuntCardID"].ToString().Trim();

                
                String strvarifycode = Request.Params["txtVarifycode"].ToString();
                if (!strvarifycode.ToUpper().Equals(Session["VARIFYCODE"].ToString().ToUpper()))
                {
                    strResult = "验证码错误!";
                    return strResult;
                }
                if (!bankcardnorp.Equals(bcd.BankCardNo))
                {
                    strResult = "两次卡号输入不一致!!";
                    return strResult;
                }
                bcd.AccuntId = loginuser.AccuntId;
                bcd.BankCardSafeCode = "";
                AccuntSer acser = new AccuntSer();
                Boolean bcacheck = acser.CheckBackAccuntDouble("BankCardNo", bcd.BankCardNo);
                if (bcacheck)
                {
                    strResult = "该卡号已经被添加!";
                    return strResult;
                }else
                {
                    bcd.LastModifyer = Session["USERID"].ToString();
                    bcd.LastModifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    bcd.BankCardKey = "BANK" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + DateTime.Now.Millisecond;
                    Boolean isadd=acser.AddBankCardAccunt(bcd, Session["USERID"].ToString());
                    if (isadd)
                    {
                        strResult = "添加成功";
                    }else
                    {
                        strResult = "添加失败";
                    }
                }
            }
            catch(Exception ex)
            {
                return ex.Message;
            }


            return strResult;
        }
    }
}